package com.btkjgc.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.AccountInfoModel;
import com.btkjgc.models.extend.RechargeRecordModel;
import com.btkjgc.service.intf.RechargeManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class RechargeManageController
{
	static Logger logger = Logger.getLogger(RechargeManageController.class);
	
	@Autowired
	private RechargeManageService_Intf rechargeManageService;
	
	@RequestMapping(value = "recharge_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,Integer type)
	{
		ModelAndView mv = new ModelAndView("recharge_manage");
		mv.addObject("type", type);
		listrecord(request,mv);
		
		return mv;
	}
	
	@SuppressWarnings("unchecked")
	public String listrecord(HttpServletRequest request, ModelAndView mv)
	{
		//个人账户
		String begintime = request.getParameter("begintime");
		String endtime = request.getParameter("endtime");
		String phone = request.getParameter("phone");
		String siteid = request.getParameter("siteid");
		String rechargeType = request.getParameter("rechargeType");
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}
		
		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null)
		{
			pageIndex = Integer.parseInt(page);
		}
		
		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1={"createdate","d>",begintime};
		Object[] arr2={"createdate","d<",endtime};
		Object[] arr3={"phone","like",phone};
		Object[] arr4={"siteid","=",siteid};
		Object[] arr5={"rechargeType","=",rechargeType};
		paramList.add(arr1);
		paramList.add(arr2);
		paramList.add(arr3);
		paramList.add(arr4);
		paramList.add(arr5);
		
		String sql1 = " select * from\n" + 
					  " (select rrt.*,aut.name as username,aut.account,aut.tel as phone,\n" +
					  " ecst.name,dt1.value,dt2.value as way,adm.username as opername\n" +
					  " FROM\n" +
					  " recharge_record_t rrt\n" +
					  " LEFT JOIN account_user_t aut ON aut.id=rrt.accountid\n" +
					  " LEFT JOIN electric_charging_site_t ecst ON ecst.id=rrt.siteid\n" +
					  " LEFT JOIN (SELECT * FROM dictionary_t WHERE name='RechargeType')dt1 ON dt1.key=rrt.rechargeType\n" +
					  " LEFT JOIN (SELECT * FROM dictionary_t WHERE name='RechargeWay')dt2 ON dt2.key=rrt.rechargeWay\n" +
					  " LEFT JOIN admin_user_t adm ON adm.id=rrt.operid WHERE aut.type=0 and rrt.del=0)a\n" +
					  " WHERE a.del=0";
		
		sql1 = rechargeManageService.getwhereSQL(sql1, paramList, sortfield);
		
		//获取分页查询的数据结果
		totalRecord = rechargeManageService.getCount(sql1, paramList);
		
		List<RechargeRecordModel> privateRechargeRecord = rechargeManageService.getList(
				RechargeRecordModel.class, paramList, sql1, pageIndex, 15, sortfield);
		
		//设置后台用户界面所需的其他数据
		rechargeManageService.setListData(request,mv,pageIndex,totalRecord,privateRechargeRecord);
		
		//公司账号
		String beginTime = request.getParameter("beginTime");
		String endTime = request.getParameter("endTime");
		String accountid = request.getParameter("accountid");
		String page1 = request.getParameter("page1");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}
		
		int pageIndex1 = 0;
		int totalRecord1 = 0;
		if (page1 != null)
		{
			pageIndex1 = Integer.parseInt(page1);
		}
		
		List<Object[]> paramList1 = new ArrayList<Object[]>();
		Object[] arr6={"createdate","d>",beginTime};
		Object[] arr7={"createdate","d<",endTime};
		Object[] arr8={"accountid","=",accountid};
		paramList1.add(arr6);
		paramList1.add(arr7);
		paramList1.add(arr8);
		
		String sql2 = " select * from\n" + 
				  	  " (select rrt.*,aut.name as username,aut.account,aut.tel as phone,\n" +
				  	  " ecst.name,dt.value,adm.username as opername\n" +
				  	  " FROM\n" +
				  	  " recharge_record_t rrt\n" +
				  	  " LEFT JOIN account_user_t aut ON aut.id=rrt.accountid\n" +
				  	  " LEFT JOIN electric_charging_site_t ecst ON ecst.id=rrt.siteid\n" +
				  	  " LEFT JOIN (SELECT * FROM dictionary_t WHERE name='RechargeType')dt ON dt.key=rrt.rechargeType\n" +
				  	  " LEFT JOIN admin_user_t adm ON adm.id=rrt.operid WHERE aut.type=1 and rrt.del=0)a\n" +
				  	  " WHERE a.del=0";
		
		sql2 = rechargeManageService.getwhereSQL(sql2, paramList1, sortfield);
		
		//获取分页查询的数据结果
		totalRecord1 = rechargeManageService.getCount(sql2, paramList1);
		
		List<RechargeRecordModel> companyRechargeRecord = rechargeManageService.getList(
				RechargeRecordModel.class, paramList1, sql2, pageIndex, 15, sortfield);
		
		//设置后台用户界面所需的其他数据
		rechargeManageService.setListData1(request, mv, pageIndex1, totalRecord1, companyRechargeRecord);
		
		return sql1+":"+sql2;
		
	}
	
	@RequestMapping(value = "get_accountinfo_by_id")
	@ResponseBody
	public AccountInfoModel getAccountinfoById(Integer accountid) {
		
		String sql = " SELECT\n" +
					 " aut.account,ait.available_balance\n" +
					 " FROM\n" +
					 " account_info_t ait\n" +
					 " LEFT JOIN account_user_t aut ON aut.id=ait.accountid\n" +
					 " WHERE ait.accountid = ?";
		
		AccountInfoModel accountInfoModel = (AccountInfoModel) HibernateUtil
				.executeSQLQueryObject(sql, AccountInfoModel.class, accountid);
		
		return accountInfoModel;
	}
	
	@RequestMapping(value = "get_printinfo_by_id")
	@ResponseBody
	public RechargeRecordModel getPrintInfoById(Integer id) {
		
		String sql = " select * from\n" + 
			  	  	 " (select rrt.*,aut.name as username,aut.account,aut.tel as phone,\n" +
			  	  	 " ecst.name,dt.value,adm.username as opername,ait.available_balance as availableBalance,\n" +
			  	  	 " cat.carbrand\n" +
			  	  	 " FROM\n" +
			  	  	 " recharge_record_t rrt\n" +
			  	  	 " LEFT JOIN car_account_t cat ON cat.accountid=rrt.accountid\n" +
			  	  	 " LEFT JOIN account_info_t ait ON ait.accountid=rrt.accountid\n" +
			  	  	 " LEFT JOIN account_user_t aut ON aut.id=rrt.accountid\n" +
			  	  	 " LEFT JOIN electric_charging_site_t ecst ON ecst.id=rrt.siteid\n" +
			  	  	 " LEFT JOIN (SELECT * FROM dictionary_t WHERE name='RechargeType')dt ON dt.key=rrt.rechargeType\n" +
			  	  	 " LEFT JOIN admin_user_t adm ON adm.id=rrt.operid WHERE rrt.del=0)a\n" +
			  	  	 " WHERE a.id = ?";
		
		RechargeRecordModel rechargeRecordModel = (RechargeRecordModel) HibernateUtil
				.executeSQLQueryObject(sql, RechargeRecordModel.class, id);
		
		return rechargeRecordModel;
	}
	
	@RequestMapping(value = "update_private_recharge", method = {RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody
	AjaxObj updatePrivateRecharge(HttpServletRequest request,
			RechargeRecordModel rechargeRecordModel) {
	
		AjaxObj ajaxObj = rechargeManageService
				.updatePrivateRecharge(rechargeRecordModel);
		
		return ajaxObj;
	}
	
	@RequestMapping(value = "update_company_recharge", method = {RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody
	AjaxObj updateCompanyRecharge(HttpServletRequest request,
			RechargeRecordModel rechargeRecordModel) {
		
		AjaxObj ajaxObj = rechargeManageService
				.updateCompanyRecharge(rechargeRecordModel);
		
		return ajaxObj;
	}
	
	@RequestMapping(value = "download_recharge_record", method = { RequestMethod.GET,
			RequestMethod.POST })
	public void downloadRechargeRecord(HttpServletRequest request,HttpServletResponse response) {
		
		String export = request.getParameter("export");
		
		ModelAndView mv = new ModelAndView("recharge_manage");
		String sql[] = listrecord(request,mv).split(":");
		
		if("0".equals(export)){
			String sql1 = null;
			sql1 = sql[0];
			
			rechargeManageService.doDownloadAction(request, response, sql1);
		}else if("1".equals(export)){
			String sql2 = null;
			sql2 = sql[1];
			
			rechargeManageService.doDownloadAction(request, response, sql2);
		}
	}
	
	@RequestMapping(value = "del_recharge_record", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String DelRechargeRecord(HttpServletRequest request, String idStr){
		String[] ids = idStr.split(",");
		for (int i = 0; i < ids.length; i++) {
			int rechargeRecordId = Integer.valueOf(ids[i]);
			rechargeManageService.deleteById(rechargeRecordId);
		}
		return "redirect:/recharge_manage";
	}
	
}